package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityAppointMemberBinding
import com.haoshuang.zhouzhoubang.model.bean.TeamMemberBean
import com.haoshuang.zhouzhoubang.ui.adapter.AppointMemberAdapter
import com.haoshuang.zhouzhoubang.ui.vm.AppointMemberViewModel
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.vertical


/**
 *  门店长接个人单 指派成员
 */
class AppointMemberActivity :
    BaseVMActivity<AppointMemberViewModel, ActivityAppointMemberBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<AppointMemberActivity>()
        }
    }

    private val data = arrayListOf<TeamMemberBean.TeamMemberData>()
    private val adapter = AppointMemberAdapter(data)

    private var id = ""

    override fun initView(savedInstanceState: Bundle?) {

        id = intent.getString("id")

        binding.titleBar.title = "任务指派"
        binding.titleBar.leftImageView().click { finish() }

        binding.recyclerView.vertical()
            .adapter = adapter

        adapter.setOnItemChildClick(0, id = R.id.clickView) { view, position, item ->
            data.forEach {
                it.isChoose = it.id == item?.id
            }
            adapter.notifyDataSetChanged()
        }

        binding.tvConfirm.clickWithTrigger {
            val item = data.firstOrNull { it.isChoose == true }
            if (item == null){
                toast("请选择上门人员")
                return@clickWithTrigger
            }
            vm.personalAddAppoint(id, item.to_user_id.toString())
        }

        val user = AccountUtils.user
        binding.ivImage.load(user?.team?.team_logo)
        binding.tvTeamName.text = user?.team?.team_name

    }

    override fun initData() {
        vm.getAppointMemberList(id)
    }

    override fun startObserve() {

        vm.personalAddAppointData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess { userId ->
                dismissLoading()
                val item = data.firstOrNull { it.to_user_id.toString() == userId }
                setResult(200, Intent().apply {
                    putExtra("chooseMember", item)
                })
                finish()
            }
        }

        vm.getAppointMemberListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll((it?.team_member_data ?: mutableListOf()).filter{ it.nickname?.isNotEmpty() == true })
                adapter.notifyDataSetChanged()
            }
        }

    }

}